<?php

#Get the parent
$pageParentID = $_GET["pid"];//id of parent layer

$pageSection = $_GET["section"];//top level dirs

$moveFlag = $_GET["move"];//move module or select module

$PAGES = new pages();
	
$PAGES = PageData::getSectionPages($pageSection,$pageParentID);


//$sql_all_parents = "SELECT distinct p_parent FROM ".$PROJECT_DB_TABLES['pages']."";
//
//$current_data_all_parents = getRecord($sql_all_parents);
//
//$sql_module_level = "SELECT p_id FROM ".$PROJECT_DB_TABLES['pages']." WHERE ";
//
//$count = 0;
//
//foreach($current_data_all_parents as $key_all_parents=>$value_all_parents){
//	
//	$sql_module_level.="p_id!=\"".$value_all_parents["p_parent"]."\"";
//	
//	if(count($current_data_all_parents)!=$count+1){
//		
//		$sql_module_level.=" AND ";
//	}
//	
//	$count++;
//}
//
//$current_data_module_level = getRecord($sql_module_level);

#select kids
//$sql_get_kids = "";

//if($topflag=="true"){
//	$sql_get_kids = "SELECT ".$PROJECT_DB_TABLES['pages'].".* FROM ".$PROJECT_DB_TABLES['pages']." WHERE p_section=\"".$parent."\" AND p_parent=\"\" AND p_status>=0 AND p_status<3 ORDER BY p_sort ASC, p_name ASC";
//}
//else{
//	$sql_get_kids = "SELECT ".$PROJECT_DB_TABLES['pages'].".* FROM ".$PROJECT_DB_TABLES['pages']." WHERE p_parent=\"".$parent."\" AND p_status>=0 AND p_status<3 ORDER BY p_sort ASC, p_name ASC";	
//}

#Get data		
//$current_data_kids = getRecord($sql_get_kids);

$max_sort = 0;

#This function is to check if all the lower level page has sort
if($moveFlag=="true"){
	
	foreach($current_data_kids as $current_value_tmp){
		
		if(!is_null($current_value_tmp["p_sort"]) && !empty($current_value_tmp["p_sort"])){
			
			if($current_value_tmp["p_sort"] > $max_sort){
				
				$max_sort = $current_value_tmp["p_sort"];
			}
		}
	}
}

$output = "<table cellspacing=\"0\" align=\"left\" >";

foreach($PAGES -> getPagesArray() as $key => $obPage){

	//$module_flag=false;
//	
//	foreach($current_data_module_level as $value_current_data_module_level){
//		
//		if($current_value["p_id"]==$value_current_data_module_level["p_id"]){
//			
//			$module_flag=true;
//			
//			break;
//		}
//	}
	
	if($moveFlag=="true"){
		
		if(is_null($current_value["p_sort"])){
			
			$max_sort = $max_sort + 1;
			
			$update_sql = "UPDATE ".$PROJECT_DB_TABLES['pages']." SET p_sort=".$max_sort." WHERE p_id=\"".$current_value["p_id"]."\"";
			
			//echo $update_sql;
			
			executeSQLQuery($update_sql);
		}
		
		#this html code is for the move page
		if(count($obPage -> getData('p_children')) == 0){
			
			#If the item don`t have kids, the icon will be document.png
			$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><div class=\"admin_pages_doc\"></div></td><td style=\"border-color: white;border-style: solid;\">";
			$output.="<a onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$obPage -> getID()."','".$obPage -> getData('p_parent')."','".$obPage -> getData('p_section')."');\">".$obPage -> getData('p_name')."(".$obPage -> getID().")"."</a><br/><div align=\"left\"></div></td></tr>";
		}
		
		else{	
		
			#If the item have kids, the icon will be folder_closed.png
			$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><div class=\"admin_pages_closed_dir\" onclick=\"onlick_openPageDir(this,'".$obPage -> getData('p_section')."','".$obPage -> getID()."',true);\"></div></td><td style=\"border-color: white;border-style: solid;\">";
			$output.="<a onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$obPage -> getID()."','".$obPage -> getData('p_parent')."','".$obPage -> getData('p_section')."');\">".$obPage -> getData('p_name')."(".$obPage -> getID().")"."</a><br/><div align=\"left\"\"></div></td></tr>";
			
		}
	}
	else{
		
		#these html codes is for the page selection
		if(count($obPage -> getData('p_children')) == 0){
			
			#If the item don`t have kids, the icon will be document.png
			$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><div class=\"admin_pages_doc\"></div></td><td style=\"border-color: white;border-style: solid;\">";
			$output.="<a onclick=\"onclick_selectPage(this,'".$obPage -> getID()."','".$obPage -> getData('p_section')."','".$obPage -> getData('p_layer')."')\" style=\"text-decoration:none;\" href=\"javascript:void(0);\">".$obPage -> getData('p_name')."(".$obPage -> getID().")"."</a><br/><div align=\"left\"></div></td></tr>";
			
		}
		
		else{	
		
			#If the item have kids, the icon will be folder_closed.png
			$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><div class=\"admin_pages_closed_dir\" onclick=\"onlick_openPageDir(this,'".$obPage -> getData('p_section')."','".$obPage -> getID()."',false);\"/></td><td style=\"border-color: white;border-style: solid;\">";
			$output.="<a onclick=\"onclick_selectPage(this,'".$obPage -> getID()."','".$obPage -> getData('p_section')."','".$obPage -> getData('p_layer')."')\" style=\"text-decoration:none;\" href=\"javascript:void(0);\">".$obPage -> getData('p_name')."(".$obPage -> getID().")"."</a><br/><div></div></td></tr>";
			
		}
	}
	
}

$output .= "</table>";
//header('Access-Control-Allow-Origin: *');

addContent($output);

?>